//
//  TransactionListController.m
//  FinancialProduct
//  交易明细
//  Created by Stella on 2017/8/27.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "ProductListCell.h"
#import "UIColor+NN.h"
#import "FitterViewController.h"
#import "CKPuslichTool.h"
#import "HandleNetProduct.h"
#import "TransactionDetailController.h"
#import "YJSegmentedControl.h"
#import "TransModel.h"
#import "TransListCell.h"
#import "MJRefresh.h"
#import "UserModel.h"
#import "bottomLabelCell.h"
#import "SearchController.h"

#import "TransactionListController.h"

@interface TransactionListController ()<YJSegmentedControlDelegate>{
    int count;
    int type;
    BOOL isBottom;
}
@property (weak, nonatomic) IBOutlet UIButton *filterBt;
@property (weak, nonatomic) IBOutlet UITableView *mTableView;

@property(nonatomic,strong)NSMutableArray *DataArray;
@property(nonatomic,strong)NSString *transType;
@property(nonatomic,strong)NSString *transTime;
@property(nonatomic,strong)NSString *productType;
@property(nonatomic,strong)NSString *productName;
@property(nonatomic,strong)NSString *moneyType;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toBottom;
@property (weak, nonatomic) YJSegmentedControl * segment;

@property (weak, nonatomic) IBOutlet UIButton *LeftBt;
@property (weak, nonatomic) IBOutlet UILabel *LeftLabel;
@property (weak, nonatomic) IBOutlet UIButton *RightBt;
@property (weak, nonatomic) IBOutlet UIView *noDataView;
@property (weak, nonatomic) IBOutlet UILabel *RightLabel;
@property (weak, nonatomic) IBOutlet UIButton *threeBt;
@property (weak, nonatomic) IBOutlet UILabel *threeLabel;

@property(strong,nonatomic)FitterViewController *myFitter;
@end

@implementation TransactionListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"交易明细";
    self.noDataView.hidden=YES;
    _DataArray=[[NSMutableArray alloc] init];
    isBottom=NO;
    _transTime=@"不限";
    _transType=@"不限";
    _productType=@"";
    _productName=@"";
    _moneyType=_typeStr;
    self.toBottom.constant=0;
    self.mTableView.estimatedRowHeight=140;
    self.mTableView.rowHeight = UITableViewAutomaticDimension;
    [self initmyButton];
    _myFitter=(FitterViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"FitterViewController"];
    count=1;
    type=0;
    _mTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count=1;
        [self getDetailInfo];
    }];
    _mTableView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        count++;
        [self getDetailInfo];
    }];
    // 马上进入刷新状态
//    [self.mTableView.mj_header beginRefreshing];
    count=1;
    [self getDetailInfo];
    // Do any additional setup after loading the view.
}
- (IBAction)ChangeStatus:(UIButton *)sender {
    self.noDataView.hidden=YES;
    isBottom=NO;
    if (sender.tag==0) {
        self.LeftBt.selected=YES;
        self.RightBt.selected=NO;
        self.threeBt.selected=NO;
        self.LeftLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
        self.RightLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        self.threeLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        self.toBottom.constant=0;
        self.filterBt.hidden=YES;
        type=0;
    }else if (sender.tag==1) {
        self.filterBt.hidden=NO;
        self.LeftBt.selected=NO;
        self.RightBt.selected=YES;
        self.threeBt.selected=NO;
        self.RightLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
        self.LeftLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        self.threeLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        self.toBottom.constant=42;
        type=1;
    }else{
        self.LeftBt.selected=NO;
        self.RightBt.selected=NO;
        self.threeBt.selected=YES;
        self.threeLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
        self.RightLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        self.LeftLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        self.toBottom.constant=0;
        self.filterBt.hidden=YES;
        type=2;
    }
    
    [self reloadList];
   
}
-(void)reloadList{
    [self.DataArray removeAllObjects];
    [self.mTableView reloadData];
    count=1;
    [self getDetailInfo];
}
- (IBAction)FitterBtPress:(id)sender {
//    FitterViewController *vc = (FitterViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"FitterViewController"];
    __weak  TransactionListController   *sSelf=self;
    _myFitter.returnFilters = ^(NSString *time, NSString *category) {
        NSLog(@"time:%@  category:%@",time,category);
        sSelf.transType=category;
        sSelf.transTime=time;

         [sSelf reloadList];
    };
    [_myFitter setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [_myFitter setModalPresentationStyle:UIModalPresentationOverFullScreen];
    [self presentViewController:_myFitter animated:YES completion:nil];
}
-(void)rightButton:(UIBarButtonItem *)barBt{
    //去搜索页面
    //去搜索页面
    SearchController *vc=(SearchController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SearchController"];
    vc.returnSearchContent = ^(NSString *searchContent) {
        self.productName=searchContent;
        self.productType=@"";
        self.moneyType=_typeStr;

         [self reloadList];
    };
    vc.returnCategory = ^(NSUInteger index) {
        switch (index) {
            case 0:
                self.productType=@"";
                break;
            case 1:
                self.productType=@"A";
                break;
            case 2:
                self.productType=@"B";
                break;
            case 3:
                self.productType=@"C";
                break;
            case 4:
                self.productType=@"D";
                break;
                
            default:
                break;
        }
        self.moneyType=_typeStr;
        self.productName=@"";

         [self reloadList];
    };
    vc.returnMoneyType = ^(NSString *moneyType) {
        if ([moneyType isEqualToString:@"人民币"]) {
            self.moneyType=@"RMB";
            
        }else{
            self.moneyType=@"USD";
        }
        self.productType=@"";
        self.productName=@"";

         [self reloadList];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)initmyButton{
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"sousuoW"] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    self.toBottom.constant=0;
    self.filterBt.hidden=YES;
    
//    [self.segment removeFromSuperview];
//    NSArray *btnDataSource=@[@"在途订单",@"历史交易"];
//    //  第4部  调用创建
//    YJSegmentedControl * segment = [YJSegmentedControl segmentedControlFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50) titleDataSource:btnDataSource backgroundColor:[UIColor whiteColor] titleColor:[UIColor colorWithHexString:@"#666666"] titleFont:[UIFont systemFontOfSize:17] selectColor:[UIColor colorWithHexString:@"BB3535"] buttonDownColor:[UIColor colorWithHexString:@"BB3535"] Delegate:self];
//    // 第5步 添加到试图上
//    self.segment = segment;
//    self.segment.delegate=self;
//    [self.view addSubview:self.segment];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)getDetailInfo{
    if (isBottom && count>1) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
        return;
    }
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];

    NSDictionary *params;
    if (type==0) {
        //在途订单
        
        /*
         * SubProductName 产品名称
         * CurrencyType 币种
         * ProductTypeLevel 产品类型
         */
        params=@{@"AAAMethodName":@"Investtrade",@"CustomerID":@(mid),
                               @"SubProductName":_productName,
                               @"CurrencyType":self.typeStr,//self.typeStr,
                               @"ProductTypeLevel":_productType,
                               @"PageIndex":@(count),
                               @"PageSize":@(20)};
        //NSLog(@"在途订单params===%@",params);
     

    }else if (type==1) {
        //历史交易
        /*
         * TransactionTime 交易时间 默认不限
         * CurrencyType 币种
         * ProductTypeLevel 产品类型
         * TransactionType 交易类型 默认不限
         * WebProductName 产品名称
         */
        
        params=@{@"AAAMethodName":@"Investtradehistory",@"CustomerID":@(mid),
                               @"TransactionTime":_transTime,
                               @"TransactionType":_transType,
                               @"CurrencyType":_moneyType,
                               @"ProductTypeLevel":_productType,
                               @"WebProductName":_productName,
                               @"PageIndex":@(count),
                               @"PageSize":@(20)};
        //NSLog(@"历史交易params===%@",params);
        
    }else{
        params=@{@"AAAMethodName":@"LoseEfficacyBooking",@"CustomerID":@(mid),
                 @"SubProductName":_productName,
                 @"CurrencyType":self.typeStr,//self.typeStr,
                 @"ProductTypeLevel":_productType,
                 @"PageIndex":@(count),
                 @"PageSize":@(20)};
    }
    [HandleNetProduct getListforParams:params Success:^(NSArray *data) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
        if (count==1) {
            [self.DataArray removeAllObjects];
        }
        if (data.count>0) {
            
            [self.DataArray addObjectsFromArray:data];
            isBottom=NO;
        }else{
            isBottom=YES;
        }
        if (self.DataArray.count>0) {
            self.noDataView.hidden=YES;
           
        }else{
            self.noDataView.hidden=NO;
        }
        [self.mTableView reloadData];
    } Failure:^(NSString *errorInfo) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
    }];
 
   
 
}

//#pragma mark - Table view data source
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (type==1) {
//        return 145;
//    }
//    return 123;
//    
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (isBottom) {
        return self.DataArray.count+1;
    }
    return self.DataArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==self.DataArray.count) {
        bottomLabelCell *cell=[tableView dequeueReusableCellWithIdentifier:@"bottomLabelCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        cell.backgroundColor=[UIColor clearColor];
        cell.totalLabel.text=[NSString stringWithFormat:@"已到底部，共加载%d条信息",(int)self.DataArray.count];
        return cell;
    }
    
    if (type==1) {
        ProductListCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ProductListCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        NSDictionary *dic=self.DataArray[indexPath.section];
        RecordModel *model=[RecordModel mj_objectWithKeyValues:dic];
        cell.HisRecordModel=model;
        [cell layoutIfNeeded];
        return cell;
    }else{
        NSDictionary *info=self.DataArray[indexPath.section];
        NSString *cellStr;
        if ([info[@"ProductTypeLevel1"] isEqualToString:@"二级市场"]&&[info[@"BookingType"] isEqualToString:@"赎回"]){
            cellStr=@"TransListCell2";
        }else{
            cellStr=@"TransListCell1";
        }
        TransListCell *cell=[tableView dequeueReusableCellWithIdentifier:cellStr forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        
        cell.transModel=[TransModel mj_objectWithKeyValues:info];
        [cell layoutIfNeeded];
        return cell;
    }
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (type==1) {
        TransactionDetailController *vc=(TransactionDetailController*)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"TransactionDetailController"];
        
        NSDictionary *dic=self.DataArray[indexPath.section];
        RecordModel *model=[RecordModel mj_objectWithKeyValues:dic];
        vc.myRecordModel=model;
        [self.navigationController pushViewController:vc animated:YES];
    }
   
}

@end
